//
//  JNBottomBaseView.m
//  JiangNanMall
//
//  Created by icholab on 2020/7/15.
//  Copyright © 2020 DCloud. All rights reserved.
//

#import "JNBottomBaseView.h"
#import "UIView+JNAddtions.h"
@interface JNBottomBaseView ()
@property (nonatomic, strong) UILabel  *titleLabel;
@property (nonatomic, strong) UIView  *topView;

@property (nonatomic, strong) UIView  *baseContentView;
//@property (nonatomic, strong) UIButton  *closeBtn;
@property (nonatomic, strong) UIView  *baseView;
@property (nonatomic, assign) CGFloat  bottomHeight;
@property (nonatomic, strong) CAShapeLayer  *maskLayer;

@property (nonatomic, strong) UIButton  *submitBtn;
@property (nonatomic, strong) UIButton  *cancellBtn;

@property (nonatomic, strong) UIView  *bgView;

@end

@implementation JNBottomBaseView

- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [[UIView alloc] initWithFrame:self.bounds];
        _bgView.backgroundColor = UIColorFromRGBAlpha(0x000000,0.5);
    }
    return _bgView;
}

- (UIView *)topView{
    if (!_topView) {
        _topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, titleHeight)];
    }
    return _topView;
}

- (UIButton *)cancellBtn{
    if (!_cancellBtn) {
        _cancellBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, titleHeight, titleHeight)];
        [_cancellBtn setTitleColor:UIColorFromRGB(0x333333) forState:UIControlStateNormal];
        _cancellBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [_cancellBtn addTarget:self action:@selector(clickCancellAction) forControlEvents:UIControlEventTouchUpInside];
        [self.topView addSubview:_cancellBtn];
    }
    return _cancellBtn;
}

- (UIButton *)submitBtn{
    if (!_submitBtn) {
        _submitBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.width - titleHeight, 0, titleHeight, titleHeight)];
        [_submitBtn setTitleColor:UIColorFromRGB(0x333333) forState:UIControlStateNormal];
        _submitBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [self.topView addSubview:_submitBtn];
        [_submitBtn addTarget:self action:@selector(clickSubmitAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _submitBtn;
}

- (UIView *)baseView{
    if (!_baseView) {
        _baseView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, self.bottomHeight)];
        _baseView.backgroundColor = [UIColor whiteColor];
    }
    return _baseView;
}

- (CAShapeLayer *)maskLayer{
    if (!_maskLayer) {
        CGRect bounds = CGRectMake(0, 0, self.width, self.bottomHeight);
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(15, 15)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        _maskLayer = maskLayer;
        maskLayer.frame = bounds;
        maskLayer.path = maskPath.CGPath;
    }
    return _maskLayer;
}

- (UIView *)baseContentView{
    if (!_baseContentView) {
        _baseContentView = [[UIView alloc] init];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickTap)];
        [_baseContentView addGestureRecognizer:tap];
    }
    return _baseContentView;
}

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = UIColorFromRGB(0x222222);
        _titleLabel.font = [UIFont systemFontOfSize:16];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}

//- (UIButton *)closeBtn{
//    if (!_closeBtn) {
//        _closeBtn = [[UIButton alloc] init];
//        [_closeBtn addTarget:self action:@selector(clickClose) forControlEvents:UIControlEventTouchUpInside];
//        [_closeBtn setImage:[UIImage imageNamed:@"nav_close"] forState:UIControlStateNormal];
//        _closeBtn.frame = CGRectMake(0, 0, 20, 20);
//        _closeBtn.imageEdgeInsets = UIEdgeInsetsMake(2, 2, 2, 2);
//        _closeBtn.imageView.contentMode = UIViewContentModeScaleAspectFill;
//    }
//    return _closeBtn;
//}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.bgView.bounds = self.bounds;
//    self.closeBtn.x = 12;
//    self.closeBtn.y = 12;
    if (!_submitBtn) {
        self.titleLabel.width = self.width;
    }
    self.titleLabel.height = titleHeight;
    self.baseContentView.y = self.titleLabel.bottom;
    self.baseView.height = self.baseContentView.height + titleHeight;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initBaseView];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickClose)];
        [self addGestureRecognizer:tap];
    }
    return self;
}

- (void)initBaseView{
    [self addSubview:self.bgView];
    [self addSubview:self.baseView];
    [self.baseView addSubview:self.topView];
//    [self.baseView addSubview:self.closeBtn];
    [self.topView addSubview:self.titleLabel];
    [self.baseView addSubview:self.baseContentView];
}

- (void)resetBaseViewFrameWithContentView:(UIView *)contentView{
    self.baseContentView.width = contentView.width;
    self.baseContentView.height = contentView.height;
    self.baseView.height = self.baseContentView.height + titleHeight;
    
}

#pragma mark - public
- (void)addContentView:(UIView *)contentView{
    [self.baseContentView addSubview:contentView];
    [self resetBaseViewFrameWithContentView:contentView];
}

- (void)setCancelTitle:(NSString *)title{
    [self.cancellBtn setTitle:title forState:UIControlStateNormal];
    [self updateTitleFrame];
}

- (void)setCancelImage:(UIImage *)image{
    [self.cancellBtn setImage:image forState:UIControlStateNormal];
    [self updateTitleFrame];

}

- (void)setSubmitTitle:(NSString *)title{
    [self.submitBtn setTitle:title forState:UIControlStateNormal];
    [self updateTitleFrame];

}

- (void)setAttrtibuteTitle:(NSMutableAttributedString *)title{
    self.titleLabel.text = @"";
    self.titleLabel.attributedText = title;
}

- (void)setSubmitImage:(UIImage *)image{
    [self.submitBtn setImage:image forState:UIControlStateNormal];
    [self updateTitleFrame];

}

- (void)showSubmitButton{
    
}

- (void)setTitle:(NSString *)title{
    self.titleLabel.text = title;
}

- (void)clickTap{
    
}

- (void)show{
    self.hidden = NO;
    [self layoutIfNeeded];
    self.baseView.y = self.height;
    [UIView animateWithDuration:0.3 animations:^{
        self.baseView.bottom= self.height;
    }];
}

- (void)hidden{
    [UIView animateWithDuration:0.3 animations:^{
        self.baseView.y = self.height;
    }completion:^(BOOL finished) {
        self.hidden = YES;
    }];
}

- (void)clickClose{
    [self hidden];
}

- (void)setBottomCorner{
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.baseView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(30, 30)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.path = maskPath.CGPath;
    _maskLayer = maskLayer;
    self.baseView.layer.mask = self.maskLayer;
}

#pragma mark private

- (void)updateTitleFrame{
    self.titleLabel.width = self.topView.width - self.cancellBtn.width * 2;
    self.titleLabel.x = self.cancellBtn.right;
}
- (void)clickSubmitAction{
    if (self.ClickSubBlock) {
        self.ClickSubBlock();
    }
    [self hidden];
}

- (void)clickCancellAction{
    [self hidden];
}

- (UIView *)getBottomBaseView{
    return self.baseView;
}
@end
